<style>
  .header-font {
    font-size: 15px;
    font-weight: bold;
  }
  .line {
    margin: 10px 0;
    width: 100%;
    border: 0.5px solid rgb(218, 218, 218);
  }
  .table {
    margin-top: 10px;
  }
  .paging {
    float:right;
    margin-top:10px;
  }
</style>
<template>
  <div>
    <div class='header-font'>
      <Icon type="md-apps" />
      <span>成团列表</span>
    </div>
    <div class="line"></div>
    <upgoods-form
      class="product-analysis-form"
      @on-success-valid="handleSubmit">
    </upgoods-form>
    <upgoods-drawer
      @upgoods-valid="handleEdit">
    </upgoods-drawer>
    <Table
      class="table"
      :columns="columns"
      :data="historyData"
      :loading="loading"
      border>
    </Table>
    <table-page
      class="paging"
      :dataCount="dataCount"
      @change-page="changePage"
      @change-page-size="changePageSize"
      :pageSize="pageSize">
    </table-page>
  </div>
</template>
<script>
import {
  mapActions,
  mapState
} from 'vuex'
import UpgoodsDrawer from '_c/upgoods-drawer'
import UpgoodsForm from '_c/upgoods-form'
import TablePage from '_c/table-page'
export default {
  components: {
    UpgoodsForm,
    UpgoodsDrawer,
    TablePage
  },
  data () {
    return {
      page: 1,
      title: '任务管理成团列表',
      id: null,
      rows: 20,
      loading: true,
      confirmData: {},
      columns: [
        {
          title: '序号',
          type: 'index2',
          width: 80,
          align: 'center',
          render: (h, params) => {
            return h('span', params.index + (this.page - 1) * this.pageSize + 1)
          }
        },
        {
          title: '拼团发起人',
          key: 'uiname',
          minWidth: 120,
          align: 'center'
        },
        {
          title: '手机号',
          key: 'tel',
          minWidth: 120,
          align: 'center'
        },
        {
          title: '商品名称',
          key: 'gname',
          minWidth: 120,
          align: 'center'
        },
        // {
        //   title: '取货码',
        //   key: 'code',
        //   minWidth: 120,
        //   align: 'center',
        //   render: (h, params) => {
        //     return h('span', params.row.code ? params.row.code : '-')
        //   }
        // },
        {
          title: '成团状态',
          key: 'state',
          minWidth: 120,
          align: 'center',
          render: (h, params) => {
            return h('span', params.row.state === 0 ? '未成团' : params.row.state === 1 ? '已成团' : params.row.state === 2 ? '已购买' : '已取消')
          }
        },
        {
          title: '成团时间',
          key: 'ctime',
          minWidth: 120,
          align: 'center'
        },
        // {
        //   title: '发起时间',
        //   key: 'time',
        //   minWidth: 120,
        //   align: 'center'
        // },
        // {
        //   title: '状态',
        //   key: 'state',
        //   align: 'center',
        //   minWidth: 120,
        //   render: (h, params) => {
        //     return h(
        //       'span',
        //       {},
        //       params.row.state === 0 ? '未上架' : params.row.state === 1 ? '已上架' : '-'
        //     )
        //   }
        // },
        {
          title: '操作',
          key: 'cz',
          minWidth: 150,
          align: 'center',
          render: (h, params) => {
            return h('div', [
              h('Button', {
                props: {
                  type: 'primary',
                  size: 'small',
                  ghost: 'ghost'
                },
                style: {
                  marginRight: '5px'
                },
                on: {
                  click: () => {
                    this.handleClick(params.row.id, params.row)
                  }
                }
              }, '详情')
              // h('Button', {
              //   props: {
              //     type: 'error',
              //     size: 'small',
              //     ghost: 'ghost'
              //   },
              //   style: {
              //     marginRight: '5px'
              //   },
              //   on: {
              //     click: () => {
              //       this.handleDelete(params.row.id)
              //     }
              //   }
              // }, '删除')
            ])
          }
        }
      ]
    }
  },
  computed: {
    ...mapState({
      upGoodsList: state => state.upGoods.upGoodsList,
      dataCount: state => state.upGoods.upGoodsDataCount,
      historyData: state => state.upGoods.upGoodsHistoryData,
      pageSize: state => state.upGoods.upGoodsPageSize,
      row: state => state.upGoods.row
    })
  },
  methods: {
    ...mapActions([
      'getUpGoodsList',
      'getUpGoodsInfoList',
      'editUpGoodsData',
      'getUpGoodsCurrentRow',
      'updateCustomerDrawerState',
      'changeUpGoodsPage',
      'changeUpGoodsPageSize'
    ]),
    handleSubmit (playload) {
      this.loading = true
      this.playload = playload
      this.getUpGoodsList({
        ...playload,
        rows: this.rows
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    },
    handleClick (id, row) {
      // console.log(row)
      let newRow = {}
      // this.id = id
      if (row) {
        newRow = Object.assign({}, row)
      }
      this.getUpGoodsCurrentRow(newRow)
      this.getUpGoodsInfoList({ id: row.id })
      // console.log(this.row)
      this.updateCustomerDrawerState(true)
    },
    async handleEdit (playload) {
      this.loading = true
      const resp = await this.editUpGoodsData({
        id: this.row.id,
        state: 2
      })
      if (resp.data.state === '000') {
        this.$Message.success('编辑成功')
        this.getUpGoodsList({
          page: this.page,
          rows: this.rows
        }).then((res) => {
          if (res.data.state === '000') {
            this.loading = false
          }
        })
      } else {
        this.$Message.error(resp.data.msg)
      }
      this.updateCustomerDrawerState(false)
    },
    changePage (index) {
      this.lodaing = true
      this.page = index
      this.changeUpGoodsPage(index)
      this.getUpGoodsList({
        page: index,
        rows: this.rows
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    },
    changePageSize (value) {
      this.loading = true
      this.rows = value
      this.changeUpGoodsPageSize(value)
      this.changPage(this.page)
      this.getUpGoodsList({
        rows: value
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    }
  },
  created () {
    this.getUpGoodsList({}).then((res) => {
      if (res.data.state === '000') {
        this.loading = false
      }
    })
  }
}
</script>
